# vPresent is an immersive presentation creation and display application.
# Copyright (C) 2007 by Infiscape Corporation
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

from PyQt4 import QtCore


class Cluster(QtCore.QObject):
   '''
   This class represents the cluster as a sequence of cluster.node.Node
   objects. The interface for this class mimics a Python sequence, which
   makes its use convenient. Internally, Qt signals are emitted when the data
   changes, which makes it well-behaved in the context of a GUI.
   '''

   def __init__(self, parent = None):
      QtCore.QObject.__init__(self, parent)
      self.nodes = []

   def addNode(self, node):
      self.append(node)

   def removeNode(self, node):
      self.remove(node)

   def append(self, node):
      self.nodes.append(node)
      self.emit(QtCore.SIGNAL("nodeAdded(QObject*)"), node)

   def insert(self, index, node):
      self.nodes.insert(index, node)
      self.emit(QtCore.SIGNAL("nodeInserted(int,QObject*)"), index, node)

   def extend(self, iterable):
      self.nodes.extend(iterable)
      for n in iterable:
         self.emit(QtCore.SIGNAL("nodeAdded(QObject*)"), n)

   def remove(self, node):
      index = self.nodes.index(node)
      self.nodes.remove(node)
      self.emit(QtCore.SIGNAL("nodeRemoved(int,QObject*)"), index, node)

   def count(self, node):
      return self.nodes.count(node)

   def index(self, node):
      return self.nodes.index(node)

   def pop(self, index = None):
      if index is None:
         index = len(self.nodes) - 1

      node = self.nodes.pop(index)

      if node is not None:
         self.emit(QtCore.SIGNAL("nodeRemoved(int,QObject*)"), index, node)

      return node

   def reverse(self):
      self.nodes.reverse()
      self.emit(QtCore.SIGNAL("nodesSorted()"))

   def sort(self, cmpfunc = None):
      self.nodes.sort(cmpfunc)
      self.emit(QtCore.SIGNAL("nodesSorted()"))

   def __len__(self):
      return len(self.nodes)

   def __getitem__(self, index):
      return self.nodes[index]

   def __setitem__(self, index, node):
      old_node = self.nodes[index]
      self.nodes[index] = node
      self.emit(QtCore.SIGNAL("nodeChanged(int,QObject*,QObject*)"), index,
                old_node, node)

   def __delitem__(self, index):
      node = self.nodes[index]
      del self.nodes[index]
      self.emit(QtCore.SIGNAL("nodeRemoved(int,QObject*)"), index, node)

   def __iter__(self):
      return self.nodes.__iter__()

   def __contains__(self, item):
      return item in self.nodes

   def __add__(self, l):
      self.nodes.__add__(l)
      for n in l:
         self.emit(QtCore.SIGNAL("nodeAdded(QObject*)"), n)

   def __iadd__(self, l):
      self.nodes.__iadd__(l)
      for n in l:
         self.emit(QtCore.SIGNAL("nodeAdded(QObject*)"), n)

   def __mul__(self, l):
      self.nodes.__mul__(l)
      print "??? What signal to emit ???"

   def __imul__(self, l):
      self.nodes.__imul__(l)
      print "??? What signal to emit ???"

   def __getslice__(self, i, j):
      return self.nodes[i:j]

   def __str__(self):
      return str(self.nodes)
